within Modelica.Electrical.QuasiStatic.SinglePhase.Ideal;
model IdealCommutingSwitch "Ideal commuting switch"
  import Modelica.ComplexMath.real;
  import Modelica.ComplexMath.conj;
  parameter SI.Resistance Ron(final min=0) = 1e-5 "Closed switch resistance";
  parameter SI.Conductance Goff(final min=0) = 1e-5 "Opened switch conductance";
  extends Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort(final T=
       293.15);
  QuasiStatic.SinglePhase.Interfaces.PositivePin p
    annotation (Placement(transformation(extent={{-110,-10},{-90,10}})));
  QuasiStatic.SinglePhase.Interfaces.NegativePin n2
    annotation (Placement(transformation(extent={{90,-10},{110,10}})));
  QuasiStatic.SinglePhase.Interfaces.NegativePin n1 annotation (Placement(
        transformation(extent={{90,30},{110,50}}), iconTransformation(extent={{
            90,30},{110,50}})));
  Modelica.Blocks.Interfaces.BooleanInput control "true => p--n2 connected, false => p--n1 connected"
                                                        annotation (
      Placement(transformation(
        origin={0,120},
        extent={{-20,-20},{20,20}},
        rotation=270)));
protected
  Complex s1(re(final unit="1"), im(final unit="1"));
  Complex s2(re(final unit="1"), im(final unit="1")) "Auxiliary variables";
  constant SI.ComplexVoltage unitVoltage=Complex(1, 0)
    annotation (HideResult=true);
  constant SI.ComplexCurrent unitCurrent=Complex(1, 0)
    annotation (HideResult=true);
equation
  Connections.branch(p.reference, n1.reference);
  p.reference.gamma = n1.reference.gamma;
  Connections.branch(p.reference, n2.reference);
  p.reference.gamma = n2.reference.gamma;
  p.i + n2.i + n1.i = Complex(0, 0);
  p.v - n1.v = (s1*unitCurrent)*(if (control) then 1 else Ron);
  n1.i = -(s1*unitVoltage)*(if (control) then Goff else 1);
  p.v - n2.v = (s2*unitCurrent)*(if (control) then Ron else 1);
  n2.i = -(s2*unitVoltage)*(if (control) then 1 else Goff);
  LossPower = real(p.v*conj(p.i)) + real(n1.v*conj(n1.i)) + real(n2.v*conj(
    n2.i));
  annotation (defaultComponentName="switch",
    Documentation(info="<html>
<p>
The commuting switch has a positive pin p and two negative pins n1 and n2.
The switching behaviour is controlled
by the input signal control. If control is true, the pin p is connected
with the negative pin n2. Otherwise, the pin p is connected to the negative pin n1.
</p>
<p>
In order to prevent singularities during switching, the opened
switch has a (very low) conductance Goff
and the closed switch has a (very low) resistance Ron.
The limiting case is also allowed, i.e., the resistance Ron of the
closed switch could be exactly zero and the conductance Goff of the
open switch could be also exactly zero. Note, there are circuits,
where a description with zero Ron or zero Goff is not possible.
<br><br>
<strong>Please note:</strong>
In case of useHeatPort=true the temperature dependence of the electrical
behavior is <strong>not</strong> modelled. The parameters are not temperature dependent.
</p>
<p>
<strong>Use with care:</strong>
This switch is only intended to be used for structural changes, not for fast switching sequences, due to the quasi-static formulation.
</p>
</html>"),
    Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{
            100,100}}), graphics={
        Ellipse(extent={{-44,4},{-36,-4}}, lineColor={85,170,255}),
        Line(points={{-100,0},{-44,0}}, color={85,170,255}),
        Line(points={{-37,2},{40,40}}, color={85,170,255}),
        Line(points={{40,40},{100,40}}, color={85,170,255}),
        Line(points={{40,0},{100,0}}, color={85,170,255}),
        Line(
          visible=useHeatPort,
          points={{0,-100},{0,25}},
          color={127,0,0},
          pattern=LinePattern.Dot),
        Text(
          extent={{-150,90},{150,50}},
          textString="%name",
          textColor={0,0,255})}));
end IdealCommutingSwitch;
